In [1]:
%matplotlib inline
In [2]:
# Make the plots larger
from IPython.core.pylabtools import figsize
figsize(15, 5)
In [3]:
import pandas as pd
In [4]:
import pandas.io.data as web
Get HSBC stock qoute during 2008 - 2013 from Yahoo! Finance
In [5]:
web.DataReader('0005.HK', 'yahoo', start='2008-01-01', end='2013-12-31').head()
Out[5]:
There is a short cut...
In [6]:
hsbc = web.get_data_yahoo('0005.HK', start='2008-01-01', end='2013-12-31')
hsbc.head()
Out[6]:
The Yahoo! Finace data has data for every weekdays, which includes public hoildays...
In [7]:
hsbc[hsbc['Volume'] == 0].head()
Out[7]:
To clean up the data, we excludes those data with 0 volume
In [8]:
hsbc = hsbc[hsbc['Volume'] != 0]
hsbc.head()
Out[8]:
Again, plot is much easier to understand
In [9]:
hsbc.plot(secondary_y='Volume')
Out[9]:
In here, we are only interested in adjusted close
In [10]:
hsbc = hsbc['Adj Close']
hsbc.name = '0005.HK'
hsbc.plot()
Out[10]:
Returns of HSBC
In [11]:
hsbc.pct_change().head()
Out[11]:
Drop the NaN in the first period
In [12]:
hsbc_ret = hsbc.pct_change().dropna()
hsbc_ret.head()
Out[12]:
Average return
In [13]:
hsbc_ret.mean()
Out[13]:
Volatility
In [14]:
hsbc_ret.std()
Out[14]:
Density
In [15]:
hsbc_ret.plot(kind='kde')
Out[15]:
Cummulative Return
In [16]:
(hsbc_ret + 1).prod() - 1
Out[16]:
Many technical indicators are making use of moving average. Pandas has a number of rolling statistics built-in.
Simple Moving Average (SMA)
In [17]:
hsbc_sma = pd.DataFrame({'Price': hsbc})
hsbc_sma['5 Day SMA'] = pd.rolling_mean(hsbc, window=5)
hsbc_sma['20 Day SMA'] = pd.rolling_mean(hsbc, window=20)
hsbc_sma['250 Day SMA'] = pd.rolling_mean(hsbc, window=250)
hsbc_sma
Out[17]:
In [18]:
hsbc_sma.plot()
Out[18]:
In [19]:
hsbc_sma[-90:].plot()
Out[19]:
Exponential Moving Average (EMA)
In [20]:
hsbc_ema = pd.DataFrame({'Price': hsbc})
hsbc_ema['5 Day EMA'] = pd.ewma(hsbc, span=5)
hsbc_ema['20 Day EMA'] = pd.ewma(hsbc, span=20)
hsbc_ema['250 Day EMA'] = pd.ewma(hsbc, span=250)
hsbc_ema.plot()
Out[20]:
In [21]:
hsbc_ema[-90:].plot()
Out[21]:
In [22]:
hsbc_px_ret_vol = pd.DataFrame({'Price': hsbc, 'Returns': hsbc_ret})
hsbc_px_ret_vol['5 Day Vol'] = pd.rolling_std(hsbc_ret, window=5)
hsbc_px_ret_vol.plot(subplots=True)
Out[22]:
In [23]:
hsbc_px_ret_vol.plot(kind='scatter', x='Returns', y='5 Day Vol')
Out[23]:
In [24]:
hsbc_px_ret_vol[['Returns', '5 Day Vol']].corr()
Out[24]: